/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Melee.h"
#include "StateFactory.h"
#include "Tracer.h"

#include "../Kernel.h"

extern Kernel* g_kernel;

#define TIME g_kernel->GetTime()

Melee::Melee(void) : IWeapon()
{
    SetName("Melee");
    m_attributes.Put( "weapontime", 400.0f );
    m_attributes.Put( "damage", 30 );
    m_attributes.Put( "damage2", 50 );
    m_attributes.Put( "damage3", 60 );
}

Melee::~Melee(void)
{
}

/** Fire this weapon */
void Melee::Fire()
{
    if ( IsReady() )
    {
        SetState( "WP_FIRING" );
        std::string currentState =  m_owner->GetState();

        int damage = 0;
        if( currentState == ENT_STATES[ ATTACKING1] )
        {
            m_owner->SetState( "ATTACKING2" );
            damage = m_attributes.GetInt( "damage2" );
        }
        else if ( currentState == ENT_STATES[ ATTACKING2 ] )
        {
            m_owner->SetState( "ATTACKING3" );
            damage = m_attributes.GetInt( "damage3" );
        } 
        else if ( currentState == ENT_STATES[ ATTACKING3 ] )
        {
            m_owner->SetState( "ATTACKING1" );
            damage = m_attributes.GetInt( "damage" );            
        }
        else
        {
            m_owner->SetState( "ATTACKING1" );
            damage = m_attributes.GetInt( "damage" );
        }
        Tracer tracer;
        // search for an entity in a region
        IEntity* ent = tracer.FindClosests( m_owner->GetReachRectangle(), m_owner );
        if ( ent )  ent->Damage( -damage );

        m_weaponTime = TIME + m_attributes.GetFloat("weapontime");
    }
    else
    {
        SetState( "WP_RELOADING" );
    }
}